home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1059 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: austern@isolde.mti.sgi.com (Matt Austern)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: mutable can be removed...
  5. Date: 12 Apr 1996 16:53:28 GMT
  6. Organization: SGI
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <AUSTERN.96Apr12094908@isolde.mti.sgi.com>
  9. References: <Pine.HPP.3.91.960412122220.29403A-100000@kvark.fi.uib.no>
  10. Reply-To: austern@mti.mti.sgi.com
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. X-Nntp-Posting-Host: isolde.mti.sgi.com
  13. In-Reply-To: Igor Boukanov's message of 12 Apr 1996 08:53:37 PDT
  14. Content-Length: 1426
  15. X-Lines: 35
  16. Originator: clamage@taumet
  17.  
  18. In article <Pine.HPP.3.91.960412122220.29403A-100000@kvark.fi.uib.no> Igor Boukanov <Igor.Boukanov@fi.uib.no> writes:
  19.  
  20. > template<class T> T* remove_constness_from(const T* p)
  21. > {
  22. >    return const_cast<T*>(p);
  23. > }
  24.  
  25. ...
  26.  
  27. > And remove_constness_from is more useful than mutable because in this 
  28. > case to make any class member looks like mutable one don't even need to 
  29. > modify class definition! And of cause lines like 
  30. > "remove_constness_from(this)->i = i_;"
  31. > will show explicitly what somebody does...
  32.  
  33. It's true that you can use const_cast to modify a member of an object
  34. pointed to by a const pointer.  In fact, you don't even need
  35. const_cast: you can cast away constness using old-style casts too.  In
  36. a sense, then, mutable doesn't add new capabilities.  So why was it
  37. added?
  38.  
  39. Two reasons.  First, you can't safely cast away constness of a const
  40. object; the only case where it's safe is when you have a non-const
  41. object that you're referring to by a const pointer or reference.
  42. Second, the fact that a certain member might change even in const
  43. operations (and even when the object itself is const) is important
  44. enough that it's good to have a notation where that fact can be
  45. specified in the class declaration.  Using const_cast, by contrast,
  46. forces people to look through all of the code that uses that object if
  47. they want to discover this fact.
  48. -- 
  49. Matt Austern
  50. SGI: MTI Compilers Group
  51. austern@isolde.mti.sgi.com
  52.  
  53.  
  54.  
  55. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  56. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  57. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  58. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  59. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  60.